package com.qin.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.qin.db.DBAccess;
import com.qin.entity.Component;
import com.qin.entity.ExecuteBean;
import com.qin.enums.RetCode;
import com.qin.mapper.ComponentMapper;
import com.qin.service.ComponentService;
import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.Map;

@Service
public class ComponentServiceImpl implements ComponentService {

    @Override
    public Component queryComponentByComponentId(Component component) {

//        System.out.println("service层：queryComponentsByComponentId");

        //调用Dao层
        SqlSession sqlSession = null;
        Component component0 = null;

        try {
            sqlSession = DBAccess.getSqlSession();
            ComponentMapper componentMapper = sqlSession.getMapper(ComponentMapper.class);

            component0 = componentMapper.queryComponentByComponentId(component);

        } catch (Exception e) {
            e.printStackTrace();
        }  finally {
            if(sqlSession != null){
                sqlSession.close();
            }
        }
        return component0;
    }

    @Override
    public JSONObject executeComponent(ExecuteBean executeBean) {

        //执行组件内容
        Component component = new Component();
        component.setComponent_id(executeBean.getComponent_id());
        component.execute(executeBean);

        Map map = new HashMap();
        //拼接返回JSON
        map.put("retCode", RetCode.SUCCESS.getCode());
        map.put("retMsg", RetCode.SUCCESS.getMsg());
        JSONObject retJson = new JSONObject(map);

        return retJson;
    }


}
